package com.yanqu.road.dao.impl.activity.toyhouse;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.toyhouse.config.ToyHouseOrderConfig;
import com.yanqu.road.utils.property.PropertyHelper;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ToyHouseOrderConfigDaoImpl extends TempDao {

    private static String getToyHouseOrderConfigSql = "select * from `t_s_activity_toy_house_order` where activityId=?";

    public Map<Integer, ToyHouseOrderConfig> getToyHouseOrderConfigMap(int activityId) {
        Map<Integer, ToyHouseOrderConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getToyHouseOrderConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ToyHouseOrderConfig toyHouseOrderConfig = new ToyHouseOrderConfig();
                toyHouseOrderConfig.setOrderId(rs.getInt("orderId"));
                toyHouseOrderConfig.setName(rs.getString("name"));
                toyHouseOrderConfig.setItemDesc(rs.getString("itemDesc"));
                toyHouseOrderConfig.setMulti(rs.getInt("multi"));
                toyHouseOrderConfig.setBlockList(rs.getString("blockList"));
                toyHouseOrderConfig.setScore(rs.getInt("score"));
                toyHouseOrderConfig.setSpecParam(rs.getString("specParam"));
                toyHouseOrderConfig.setWeight(rs.getInt("weight"));
                toyHouseOrderConfig.setDesc(rs.getString("desc"));
                toyHouseOrderConfig.setBlockProp(PropertyHelper.parseStringToProperty(toyHouseOrderConfig.getBlockList(), "\\|"));
                map.put(toyHouseOrderConfig.getOrderId(), toyHouseOrderConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

}
